﻿using System;
using System.Collections.Generic;
using Me.Prettyprint.Cassandra.Serializers;
using Me.Prettyprint.Cassandra.Service;
using Me.Prettyprint.Cassandra.Utils;
using Me.Prettyprint.Hector.Api;
using Me.Prettyprint.Hector.Api.Beans;
using Me.Prettyprint.Hector.Api.Factory;
using Me.Prettyprint.Hector.Api.Query;
using Org.Apache.Cassandra.Thrift;

namespace Me.Prettyprint.Cassandra.Model.Thrift
{
    /// <summary>
    /// A query for the thrift call get_range_slices of supercolumns
    /// </summary>
    /// <typeparam name="K"></typeparam>
    /// <typeparam name="SN"></typeparam>
    /// <typeparam name="N"></typeparam>
    public class ThriftRangeSuperSlicesCounterQuery<K, SN, N> : AbstractSliceQuery<K, SN, long, OrderedCounterSuperRows<K, SN, N>>,
        RangeSuperSlicesCounterQuery<K, SN, N>
    {
        public ThriftRangeSuperSlicesCounterQuery(Keyspace keyspace, Serializer<K> keySerializer,
            Serializer<SN> sNameSerializer, Serializer<N> nameSerializer) : 
            base(keyspace, keySerializer, sNameSerializer, LongSerializer.Instance)
        {
            if (nameSerializer == null)
            {
                throw new NullReferenceException("NameSerializer name cannot be null");
            }
            NameSerializer = nameSerializer;
            KeyRange = new HKeyRange<K>(keySerializer);
        }
        public Serializer<N> NameSerializer
        {
            get;set;
        }
        private HKeyRange<K> KeyRange
        {
            get;
            set;
        }
        public override string ToString() 
        {
            return "RangeSuperSlicesQuery(" + KeyRange + base.ToStringInternal() + ")";
        }
        public new RangeSuperSlicesCounterQuery<K, SN, N> SetColumnNames(List<SN> columnNames) 
        {
            return (RangeSuperSlicesCounterQuery<K, SN, N>) base.SetColumnNames(columnNames);
        }
        public new RangeSuperSlicesCounterQuery<K, SN, N> SetRange(SN start, SN finish, bool reversed, int count)
        {
            return (RangeSuperSlicesCounterQuery<K, SN, N>) base.SetRange(start, finish, reversed, count);
        }
        public new RangeSuperSlicesCounterQuery<K, SN, N> SetColumnFamily(string cf)
        {
            return (RangeSuperSlicesCounterQuery<K, SN, N>) base.SetColumnFamily(cf);
        }
        public RangeSuperSlicesCounterQuery<K, SN, N> SetRowCount(int rowCount)
        {
            KeyRange.RowCount = rowCount;
            return this;
        }
        public RangeSuperSlicesCounterQuery<K, SN, N> SetKeys(K start, K end)
        {
            KeyRange.SetKeys(start, end);
            return this;
        }
        public override QueryResult<OrderedCounterSuperRows<K, SN, N>> Execute()
        {
            if (ColumnFamilyName == null)
            {
                throw new NullReferenceException("ColumnFamily name cannot be null");
            }
            Func<KeyspaceService, OrderedCounterSuperRows<K, SN, N>> x = (KeyspaceService ks) =>
            {
                ColumnParent columnParent = new ColumnParent(ColumnFamilyName);
                Dictionary<K, List<CounterSuperColumn>> thriftRet = KeySerializer.FromBytesDictionary(
                    ks.GetSuperRangeCounterSlices(columnParent, GetPredicate(), KeyRange.ToThrift()));
                return new OrderedCounterSuperRowsImpl<K, SN, N>(thriftRet, KeySerializer,
                    ColumnNameSerializer, NameSerializer);
            };
            return new QueryResultImpl<OrderedCounterSuperRows<K, SN, N>>(
                Keyspace.DoExecute(new KeyspaceOperationCallbackBuilder<OrderedCounterSuperRows<K, SN, N>>(x)), this);
        }
    }
}
